package org.ala.tcp_feign_common.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ala.tcp_feign_common.enums.TcpFeignMessageTypeEnums;
import org.ala.tcp_feign_common.exceptions.EncodeDecodeException;
import org.ala.tcp_feign_common.logs.LoggerFactory;
import org.ala.tcp_feign_common.msg.AbstractByteMessage;
import org.ala.tcp_feign_common.msg.IByteMessageDecoder;
import org.ala.tcp_feign_common.msg.biz.BizACKByteMessageDecoder;
import org.ala.tcp_feign_common.msg.biz.BizByteMessageDecoder;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespACKByteMessageDecoder;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespByteMessageDecoder;
import org.ala.tcp_feign_common.msg.close.CloseACKByteMessageDecoder;
import org.ala.tcp_feign_common.msg.close.CloseByteMessageDecoder;
import org.ala.tcp_feign_common.msg.heart.HeartACKByteMessageDecoder;
import org.ala.tcp_feign_common.msg.heart.HeartByteMessageDecoder;
import org.ala.tcp_feign_common.msg.login.LoginACKByteMessageDecoder;
import org.ala.tcp_feign_common.msg.login.LoginByteMessageDecoder;
import org.slf4j.Logger;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;


/**
 * 消息物理解码
 * 
 * @author 骆毅(ala)
 * @date 2024年4月18日
 */
public class MessageDecodeHandler extends ByteToMessageDecoder {
	
	
	static Logger log = LoggerFactory.getLogger("tcp-feign", "decoder");

	
	/**
	 * 消息解码器
	 */
	@SuppressWarnings("rawtypes")
	protected Map<TcpFeignMessageTypeEnums, IByteMessageDecoder> decodeMapper;
	
	
	public MessageDecodeHandler() {
		init();
	}


	/**
	 * 初始化
	 */
	public void init() {
		decodeMapper = new HashMap<>();
		
		decodeMapper.put(TcpFeignMessageTypeEnums.LOGIN, new LoginByteMessageDecoder());
		decodeMapper.put(TcpFeignMessageTypeEnums.LOGIN_ACK, new LoginACKByteMessageDecoder());
		
		decodeMapper.put(TcpFeignMessageTypeEnums.HEART, new HeartByteMessageDecoder());
		decodeMapper.put(TcpFeignMessageTypeEnums.HEART_ACK, new HeartACKByteMessageDecoder());
		
		decodeMapper.put(TcpFeignMessageTypeEnums.CLOSE, new CloseByteMessageDecoder());
		decodeMapper.put(TcpFeignMessageTypeEnums.CLOSE_ACK, new CloseACKByteMessageDecoder());
		
		decodeMapper.put(TcpFeignMessageTypeEnums.BIZ, new BizByteMessageDecoder());
		decodeMapper.put(TcpFeignMessageTypeEnums.BIZ_ACK, new BizACKByteMessageDecoder());
		
		decodeMapper.put(TcpFeignMessageTypeEnums.BIZ_RESP, new BizRespByteMessageDecoder());
		decodeMapper.put(TcpFeignMessageTypeEnums.BIZ_RESP_ACK, new BizRespACKByteMessageDecoder());
	}
	
	
	@SuppressWarnings("rawtypes")
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//		log.info("[MessageDecodeHandler decode] 消息解码 ");
		
		//	消息类型（1子节）
		byte byteType = in.readByte();
		TcpFeignMessageTypeEnums type = TcpFeignMessageTypeEnums.val(byteType);
		if (type == null) {
			throw new EncodeDecodeException("未知的消息类型，无法解码. type:" + byteType);
		}
		IByteMessageDecoder decoder = decodeMapper.get(type);
		if (decoder == null) {
			throw new EncodeDecodeException("消息类型对应的解码器未定义，无法解码. type:" + type);
		}
		
		//	消息体总长（1子节）
		int len = in.readInt();
		ByteBuf body = in.readBytes(len);
		
		//	读消息体
		AbstractByteMessage msg = decoder.decoding(body, len);
		out.add(msg);
	}

}
